/// bring together hugo and external insurance to get hourly and collapsed action data on insurance usage
///

********************************************************************************
*** hugo action relative to user creation timestamp

*** save hugo user data
use "data/int/hugo clean", clear
keep id stripecustomerid age car_year mileage makemodelyearvalue_private bound female sr22 ///
	valid_license zip cohort car_age ///
	totalagentpremium dayrate price bundle_discount
gen car_value = makemodelyearvalue_private
isid id
tempfile userdat
save `userdat'

*** combine and save hourly
use "data/cleaned/hugo/hourly hugo action", replace

merge m:1 id using "data/cleaned/hugo/hourly any insurance", assert(1 3) nogen

gen insbef = 0
gen extins = 0
gen extinsall = 0
forval i=1/7 {
	replace insbef = 1 if hourstart`i'<0 & hourend`i'>0 & ///
		!mi(hourstart`i') & !mi(hourend`i')
	replace extins = 1 if hourcr>hourstart`i' & hourcr<hourend`i' & ///
		hourstart`i'>=0 & (hourend`i'-hourstart`i')>=24*14 & ///
		!mi(hourstart`i') & !mi(hourend`i')
	replace extinsall = 1 if hourcr>hourstart`i' & hourcr<hourend`i' ///
		 & (hourend`i'-hourstart`i')>=24*14 & ///
		!mi(hourstart`i') & !mi(hourend`i')
	drop hourstart`i' hourend`i'
}

sort id

by id: egen wasins = max(insbef)
drop insbef
gen anyins = extins==1 | hasres==1
gen anyinsall = extinsall==1 | hasres==1

*for the "any insurance" outcomes,
*we define the study period as three months from user creation
replace extins = . if isthreemonthcr==0
replace anyins = . if isthreemonthcr==0

save "data/cleaned/hugo/hourly all action", replace


********************************************************************************
*** summarize insurance for collapsing

*three options
	*has insurance at any point in the 90 days after user creation
	*takes up new insurance at any point in the 90 days after user creation
	*takes up new insurance at any point in the 7 days after user creation
by id: egen anybound = max(anyinsall & hourcr<=90*24 & hourcr>=0)

foreach days in 7 10 14 90 {
	by id: egen anybound`days' = max(anyins>0 & hourcr<=`days'*24 & hourcr>=0)
	label variable anybound`days' "Take-Up Any Insurance"
}

assert anybound90==1 if anybound14==1
assert anybound10==1 if anybound7==1

by id: egen isins60cr = max(anyinsall>0 & hourcr>=24*60 & hourcr<24*61)

**** collapse to collapsed action

*keep if hour>=0
gen fullday = floor(day)
bysort id: egen lastday = max(fullday)
bysort id fullday: gen nobs=_N
*there should only be one instance of all users having just one hour in a full day (after bind)
*this is the last day of the study, and we remove it
count if nobs==1 & hour>=0
assert `r(N)'==1537
drop if nobs==1
assert nobs>=24 if hour>=0

gen isthreemonth=hour>=0
gen isthreemonthall=hour>=0

*confirm indicators are within set to appropriate time frames
bysort id: egen twomosum = sum(istwomonth)
assert twomosum>57*24 & twomosum<64*24 if !mi(hour_canceled)
bysort id: egen twomocrsum = sum(istwomonthcr)
assert twomocrsum>57*24 if !mi(hour_canceled)
assert twomocrsum<64*24 if !mi(hour_canceled)
bysort id: egen threemosum = sum(isthreemonth)
assert threemosum>85*24 & threemosum<95*24 if !mi(hour_canceled)
bysort id: egen threemocrsum = sum(isthreemonthcr)
assert threemocrsum>85*24 & threemocrsum<95*24 if !mi(hour_canceled)

assert istwomonth==0 if hour<0
assert isthreemonth==0 if hour<0

gen hasres60 = hasres if istwomonth==1
gen hascov60 = hascov if istwomonth==1

gen hasrescr = hasres==1|extins==1 if isthreemonthcr==1
gen hascovcr = hascov==1|extins==1 if isthreemonthcr==1

gen hasres60cr = hasres==1|extins==1 if istwomonthcr==1
gen hascov60cr = hascov==1|extins==1 if istwomonthcr==1

gen dayhours = 1
gen dayhours60 = dayhours if istwomonth==1
gen dayhourscr = dayhours if isthreemonthcr==1
gen dayhours60cr = dayhours if istwomonthcr==1

*see if they're insured at the end of the period
local times = "twomonth twomonthcr threemonth threemonthcr threemonthall"
foreach time of local times {
	by id: egen endhour = max(hour * (is`time'==1))
	if "`time'"=="threemonth" assert endhour>=90*24 & endhour<95*24
	if "`time'"=="twomonth" assert endhour>=60*24 & endhour<65*24
	if "`time'"=="threemonthcr" summ endhour, det
	if inlist("`time'", "twomonth","threemonth") {
		gen timetoendhour = abs(endhour-hour) if hasres==1
	}
	else {
		gen timetoendhour = abs(endhour-hour) if anyins==1
	}
	by id: egen mintimetoendhour = min(timetoendhour)
	gen `time'er = mintimetoendhour < 24
	drop endhour *timetoendhour
}

*collapse to the day level
collapse (mean) resbal hasres hasreg hascov hascovreg ///
	hasres60 hascov60 hasrescr hascovcr hasres60cr hascov60cr anyins balnocov ///
	(sum) dayhours dayhours60 dayhourscr dayhours60cr, ///
	by(id bound daily bundle fullday finalcovtoend lasthour lastadd lastday hour_canceled ///
		basedayrate hourfromcrtobind twomonther twomonthcrer threemonther threemonthaller ///
			threemonthcrer ///
		dayspurchased90 paydays* payment* bundled* num* ///
		anybound* wasins *bundler *bundlernofirst)
rename fullday day

replace hasres=hasres*(dayhours/24)
replace hasreg=hasreg*(dayhours/24)
replace hascov=hascov*(dayhours/24)
replace hascovreg=hascovreg*(dayhours/24)
replace balnocov=balnocov*(dayhours/24)
replace hasres60=hasres60*(dayhours60/24)
replace hascov60=hascov60*(dayhours60/24)
replace hasrescr=hasrescr*(dayhourscr/24)
replace hascovcr=hascovcr*(dayhourscr/24)
replace hasres60cr=hasres60cr*(dayhours60cr/24)
replace hascov60cr=hascov60cr*(dayhours60cr/24)

gen day_canceled = hour_canceled/24
drop hour_canceled

save "data/cleaned/hugo/hourly daily all action", replace

gen hourlength = lasthour/24

*collapse to the id level
collapse (sum) balancewithnocoverage90=balnocov nonzeroregularbalance90=hasreg ///
			totdayscoveragebal90=hascovreg ///
		daysnonzeroreserve_90=hasres totdayscoverage_90=hascov ///
		daysnonzeroreserve_60=hasres60 totdayscoverage_60=hascov60 ///
		daysnonzeroreserve_90cr=hasrescr totdayscoverage_90cr=hascovcr ///
		daysnonzeroreserve_60cr=hasres60cr totdayscoverage_60cr=hascov60cr ///
		totdaysinsurance=anyins, ///
			by(id bound daily bundle anybound* wasins day_canceled ///
				basedayrate hourfromcrtobind dayspurchased90 *bundler *bundlernofirst ///
				twomonther twomonthcrer threemonther threemonthaller ///
					threemonthcrer paydays* payment* bundled* num*)
			
list id if bound==1 & anybound==0

rename totdayscoverage_60 totdayscoverage60
rename daysnonzeroreserve_60 daysnonzeroreserve60
rename totdayscoverage_90 totdayscoverage90
rename daysnonzeroreserve_90 daysnonzeroreserve90
rename totdayscoverage_90cr totdayscoverage90cr
rename daysnonzeroreserve_90cr daysnonzeroreserve90cr
rename totdayscoverage_60cr totdayscoverage60cr
rename daysnonzeroreserve_60cr daysnonzeroreserve60cr

***label variables
label variable daily "Pay-As-You-Go"
label variable bound "Take-Up"

label variable dayspurchased90 "Days Purchased"
label variable totdayscoverage60 "Days Insured"
label variable totdayscoverage90 "Days Insured"
label variable daysnonzeroreserve60 "Days with Hugo Coverage"
label variable daysnonzeroreserve90 "Days with Hugo Coverage"
label variable daysnonzeroreserve60cr "Days with Any Coverage"
label variable daysnonzeroreserve90cr "Days with Any Coverage"

label variable nonzeroregularbalance90 "Days with Nonzero Regular Balance"
label variable totdayscoveragebal90 "Days with Coverage and Nonzero Regular Balance"

label variable threemonther "Insured End of Study"
label variable threemonthaller "Insured End of Study (Any)"
label variable twomonther "Insured End of Study"
label variable threemonthcrer "Insured End of Study"
label variable twomonthcrer "Insured End of Study"

merge 1:1 id using `userdat', assert(3) nogen

save "data/cleaned/hugo/hourly collapsed action", replace

beep
